package com.zeyi.web.controller.system;

import cn.hutool.core.lang.Validator;
import com.zeyi.account.service.UserValidateService;
import com.zeyi.common.core.controller.BaseController;
import com.zeyi.common.core.domain.AjaxResult;
import com.zeyi.common.core.domain.model.RegisterBody;
import com.zeyi.common.utils.StringUtils;
import com.zeyi.framework.web.service.SysRegisterService;
import com.zeyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 注册验证
 *
 * @author ruoyi
 */
@RestController
public class SysRegisterController extends BaseController {
    @Autowired
    private SysRegisterService registerService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private UserValidateService userValidateService;

    @PostMapping("/register")
    public AjaxResult register(@RequestBody RegisterBody user,
                               @RequestParam(value = "registerType", required = false) String registerType) {
        if (!("true".equals(configService.selectConfigByKey("sys.account.registerUser")))) {
            return error("当前系统没有开启注册功能！");
        }
        String msg = registerService.register(user, registerType);
        return StringUtils.isEmpty(msg) ? success() : error(msg);
    }

    /**
     * 发送手机验证码
     */
    @GetMapping("/register/phone/code")
    public AjaxResult sendPhoneCode(@RequestParam String phoneNumber) {
        Validator.validateMobile(phoneNumber, "手机号码不正确");
        userValidateService.sendPhoneCode(phoneNumber);
        return success();
    }

    /**
     * 发送邮箱验证码
     */
    @GetMapping("/register/email/code")
    public AjaxResult sendEmailCode(@RequestParam String email) {
        Validator.validateEmail(email, "邮箱不正确");
        userValidateService.sendEmailCode(email);
        return success();
    }
}
